package com.swak.algs;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 链表的翻转
 * 
 * @author lifeng
 */
public class Code06 {

	@Getter
	@Setter
	@Accessors(chain = true)
	static class Code06_Node {
		int no;
		Code06_Node next;
	}

	static void printNodes(Code06_Node head) {
		while (head != null) {
			System.out.print(head.no + "\t");
			head = head.next;
		}
		System.out.println();
	}

	static Code06_Node flipNodes(Code06_Node head) {
		Code06_Node prev = null;
		Code06_Node next = null;
		while (head != null) {

			/*
			 * 保存下一个
			 */
			next = head.next;

			/*
			 * 让head 指向上一个
			 */
			head.next = prev;

			/*
			 * 继续下一个
			 */
			prev = head;
			head = next;
		}
		return prev;
	}

	public static void main(String[] args) {
		Code06_Node head = new Code06_Node().setNo(1);
		head.next = new Code06_Node().setNo(2);
		head.next.next = new Code06_Node().setNo(3);

		// 打印
		printNodes(head);

		// 翻转
		Code06_Node flip = flipNodes(head);

		// 打印
		printNodes(flip);
	}
}
